package shiqi.db.user;

import shiqi.db.Db;
import shiqi.model.User;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: ascend
 * @description: 模仿数据库
 * @author: shiqi
 * @create: 2022-03-17 16:15
 **/
public class UserDb implements Db<User, Long> {

    private List<User> userDataSource = new ArrayList<>();

    @Override
    public void initData() {
        User shiqi = new User(1L, "shiqi", "123456");
        User ljh = new User(2L, "ljh", "a123456");
        userDataSource.add(shiqi);
        userDataSource.add(ljh);
    }

    @Override
    public User search(User user) throws IllegalAccessException {
        User result = null;
        for(User data: userDataSource) {
            if (data.isSame(user)) {
                result = data;
                break;
            }
        }
        return result;
    }

    @Override
    public User searchById(Long id) {
        User result = null;
        for(User data: userDataSource) {
            if (data.getUserId().longValue() == id.longValue()) {
                result = data;
                break;
            }
        }
        return result;
    }


}
